package com.techwells.learningkites.adapter;

import java.io.Serializable;
import java.util.HashMap;
import java.util.List;

import com.lidroid.xutils.BitmapUtils;
import com.techwells.learningkites.R;
import com.techwells.learningkites.controller.SegmentDetailActivity;
import com.techwells.learningkites.networkaccess.KitesRequestUrl;
import com.techwells.learningkites.networkservice.KitesServiceMediator;
import com.techwells.learningkites.networkservice.model.Comment;
import com.techwells.learningkites.networkservice.model.VideoCategory;
import com.techwells.learningkites.networkservice.model.CourseCategory;
import com.techwells.learningkites.networkservice.model.Word;

import android.content.Context;
import android.media.AudioManager;
import android.media.SoundPool;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RatingBar;
import android.widget.TextView;

public class WordListAdapter extends BaseAdapter {
	
	private Context mContext;
	private LayoutInflater mInflater;
	private List<Word> mList;
	private SoundPool soundPoolEn,soundPoolUs;

	
	public WordListAdapter(Context mContext, List<Word> mList) {
		super();
		this.mContext = mContext;
		mInflater =(LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		this.mList = mList;
		this.soundPoolEn = new SoundPool(25, AudioManager.STREAM_MUSIC, 100);
		this.soundPoolUs = new SoundPool(25, AudioManager.STREAM_MUSIC, 100);
		initSoundPool();
	}

	private void initSoundPool() {
		for (int i = 0; i < mList.size(); i++) {
			soundPoolEn.load(KitesRequestUrl.BASE_URL_IMG + mList.get(i).voiceEn, 1);
			soundPoolUs.load(KitesRequestUrl.BASE_URL_IMG + mList.get(i).voiceUs, 1);
		}
		
	}

	@Override
	public int getCount() {
		// TODO Auto-generated method stub
		return mList.size();
	}

	@Override
	public Object getItem(int arg0) {
		// TODO Auto-generated method stub
		return mList.get(arg0);
	}

	@Override
	public long getItemId(int position) {
		// TODO Auto-generated method stub
		return position;
	}

	@Override
	public View getView(final int position, View convertView, ViewGroup parent) {
		CellHolder cellHolder;
        if (convertView == null) {
        	cellHolder = new CellHolder();
        		convertView = mInflater.inflate(R.layout.item_wordlist, null);
        		cellHolder.seq = (TextView) convertView.findViewById(R.id.seq);
        		cellHolder.word = (TextView) convertView.findViewById(R.id.word);
        		cellHolder.soundEn = (TextView) convertView.findViewById(R.id.sound_en);
        		cellHolder.soundUs = (TextView) convertView.findViewById(R.id.sound_us);
        		cellHolder.collins = (TextView) convertView.findViewById(R.id.collins);
        		cellHolder.explanation = (TextView) convertView.findViewById(R.id.explanation);
        		cellHolder.wordPlayEn = (LinearLayout) convertView.findViewById(R.id.play_sound_en);
        		cellHolder.wordPlayUs = (LinearLayout) convertView.findViewById(R.id.play_sound_us);
        		cellHolder.favorite = (ImageView) convertView.findViewById(R.id.img_favorite);
                convertView.setTag(cellHolder);

        } else {
        	cellHolder = (CellHolder) convertView.getTag();
        }  
        
        if (position < 10) {
        	cellHolder.seq.setText((position+1) + "." + "\t");
		}else {
			cellHolder.seq.setText((position+1) + ".");
		}
        
        cellHolder.word.setText(mList.get(position).word);
        cellHolder.soundEn.setText(mList.get(position).soundmarkEn);
        cellHolder.soundUs.setText(mList.get(position).soundmarkUs);
        cellHolder.collins.setText(mList.get(position).levels.get(0));
        cellHolder.explanation.setText(mList.get(position).explaination);
        
        cellHolder.wordPlayEn.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				soundPoolEn.play(position+1, 1, 1, 1, 0, 1);
			}
		});
        cellHolder.wordPlayUs.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				soundPoolUs.play(position+1, 1, 1, 1, 0, 1);
			}
		});
        
        cellHolder.favorite.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				HashMap<String, Object> argMap = new HashMap<String, Object>();	
				argMap.put("word", mList.get(position).word);
				((SegmentDetailActivity) mContext).doTask(KitesServiceMediator.SERVICE_ADD_WORDS,argMap);
				
			}
		});
        
		return convertView;
	}

	
	public static class CellHolder implements Serializable{
		TextView seq,word,soundEn,soundUs,collins,explanation;
		LinearLayout wordPlayEn,wordPlayUs;
		ImageView favorite;
    }
}
